﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Settings Flyout item template is documented at http://go.microsoft.com/fwlink/?LinkId=273769

namespace CompleteReader.Settings
{
    public sealed partial class ViewerSettings : SettingsFlyout
    {
        private Windows.Storage.ApplicationDataContainer _RoamingSettings;

        public ViewerSettings()
        {
            this.InitializeComponent();

            _RoamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

            if (_RoamingSettings.Values.ContainsKey(pdftron.PDF.Tools.ToolManager.AnnotationAuthorNameSettingsString))
            {
                AuthorNameTextBox.Text = _RoamingSettings.Values[pdftron.PDF.Tools.ToolManager.AnnotationAuthorNameSettingsString] as string;
            }

            ButtonsStayDownToggle.IsOn = Settings.ButtonsStayDown;
            EmailSignatureTextBox.Text = Settings.EmailSignature;
        }

        private void AuthorNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _RoamingSettings.Values[pdftron.PDF.Tools.ToolManager.AnnotationAuthorNameSettingsString] = AuthorNameTextBox.Text;
        }

        private void RememberLastPage_Toggled(object sender, RoutedEventArgs e)
        {
            ToggleSwitch rememberLastPageToggle = sender as ToggleSwitch;
            if (rememberLastPageToggle.IsOn == true)
            {
                Settings.RememberLastPage = true;
            }
            else
            {
                Settings.RememberLastPage = false;
            }
        }

        private void ButtonsStayDown_Toggled(object sender, RoutedEventArgs e)
        {
            ToggleSwitch buttonsStayDownToggle = sender as ToggleSwitch;
            if (buttonsStayDownToggle.IsOn == true)
            {
                Settings.ButtonsStayDown = true;
            }
            else
            {
                Settings.ButtonsStayDown = false;
            }
            Frame frame = Window.Current.Content as Frame;
            if (frame != null)
            {
                ViewerPage viewPage = frame.Content as ViewerPage;
                if (viewPage != null)
                {
                    viewPage.SetButtonsStayDown(buttonsStayDownToggle.IsOn);
                }
            }
        }

        private void EmailSignatureTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Settings.EmailSignature = EmailSignatureTextBox.Text;
        }
    }
}
